package com.furkid.service.controller;

import com.furkid.service.constant.AjaxResult;
import com.furkid.service.constant.QiniuUtil;
import com.furkid.service.domain.UploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/upload")
public class UploadController {
    @Autowired
    private QiniuUtil qiniuUtil;

    @PostMapping
    public AjaxResult upload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "dir", defaultValue = "uploads") String dir
    ) {
        try {
            UploadResult result = qiniuUtil.upload(file, dir);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/person/avatar")
    public AjaxResult uploadPersonAvatar(@RequestParam("file") MultipartFile file) {
        try {
            UploadResult result = qiniuUtil.upload(file, "head");
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("头像上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/pet/avatar")
    public AjaxResult uploadPetAvatar(@RequestParam("file") MultipartFile file) {
        try {
            UploadResult result = qiniuUtil.upload(file, "pet_head");
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("头像上传失败：" + e.getMessage());
        }
    }
}
